package com.zh.common.view

import android.content.Context
import android.text.InputFilter
import android.text.InputType
import android.text.Spanned
import android.util.AttributeSet
import android.view.Gravity
import com.zh.common.R
import java.util.regex.Pattern

class MoneyEditView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyle: Int = 0
) : androidx.appcompat.widget.AppCompatEditText(context, attrs, defStyle) {

    init {
        isFocusable = false
        isFocusableInTouchMode = true
        gravity = Gravity.CENTER_VERTICAL

        val a = context.obtainStyledAttributes(attrs, R.styleable.MoneyEditView, defStyle, 0)
        val canNegative = a.getBoolean(R.styleable.MoneyEditView_mdv_canNegative, false)
        val maxLen = a.getInteger(R.styleable.MoneyEditView_mdv_maxLen, 9)
        val maxPoi = a.getInteger(R.styleable.MoneyEditView_mdv_maxPoi, 2)
        makeMoneyFormat(canNegative, maxLen, maxPoi)
        a.recycle()
    }

    /**
     * 金额输入 数字输入键盘
     * @param canNegative  能输入负数
     * @param maxLen  默认9位数字(整数部分)
     * @param maxPoi  限制2位小数(小数部分)
     */
    fun makeMoneyFormat(canNegative: Boolean = false, maxLen: Int = 9, maxPoi: Int = 2) {
        this.inputType =
            if (canNegative) InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_SIGNED or InputType.TYPE_NUMBER_FLAG_DECIMAL
            else InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_DECIMAL
        if (maxPoi < 0) {
            this.filters = arrayOf(InputMoneyFilter(canNegative, maxLen, 2))
        } else {
            this.filters = arrayOf(InputMoneyFilter(canNegative, maxLen, maxPoi))
        }
    }

    private inner class InputMoneyFilter(canNegative: Boolean, maxLen: Int, maxPoi: Int) :
        InputFilter {

        private var pattern: String

        //包含maxLen, 得-1
        init {
            pattern =
                "(([0]|(0[.]\\d{0,$maxPoi}))|([1-9]\\d{0,${maxLen - 1}}(([.]\\d{0,$maxPoi})?)))?"
            if (canNegative) {
                pattern =
                    "(\\-)?(([0]|(0[.]\\d{0,$maxPoi}))|([1-9]\\d{0,${maxLen - 1}}(([.]\\d{0,$maxPoi})?)))?"
            }
        }

        override fun filter(
            source: CharSequence, start: Int, end: Int, dest: Spanned, dstart: Int, dend: Int
        ): CharSequence? {
            //获取改变之后的字符串
            var text = ""
            if (end > start) {
                //插入字符串
                if (dstart > 0) {
                    text = dest.toString().substring(0, dstart)
                }
                text += source
                if (dstart < dest.toString().length) {
                    text += dest.toString().substring(dstart)
                }
            } else {
                //删除字符串
                if (dstart > 0) {
                    text = dest.toString().substring(0, dstart)
                }
                if (dest.toString().length > dstart + (dend - dstart)) {
                    text += dest.toString().substring(dstart + (dend - dstart))
                }
            }
            return if (Pattern.matches(pattern, text)) {
                null
            } else {
                ""
            }
        }
    }
}